package com.jandan.ui.client;

import java.util.List;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.ToolTip;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.jandan.ui.client.util.ClientUtil;
import com.jandan.ui.model.CnAndSentence;
import com.jandan.ui.model.LearnedWord;

public class LatestLearnedWordPanel extends Panel {
	private FlexTable latestLearnedWordTable;
	private ILessonOperator lessonOperator;

	public LatestLearnedWordPanel(ILessonOperator lessonOperator) {
		this.lessonOperator = lessonOperator;
		this.setTitle("最近所学单词");
		this.setLayout(new HorizontalLayout(20));
		this.setAutoScroll(true);

		latestLearnedWordTable = new FlexTable();

		this.add(latestLearnedWordTable);

		latestLearnedWordTable.setText(0, 0, "单词");
		latestLearnedWordTable.setText(0, 1, "学习次数");
		latestLearnedWordTable.setText(0, 2, "熟悉度");
		latestLearnedWordTable.setCellSpacing(5);
		latestLearnedWordTable.setCellPadding(5);
		latestLearnedWordTable.getRowFormatter().addStyleName(0,
				"watchListHeader");

		latestLearnedWordTable.getColumnFormatter().addStyleName(0,
				"firstColumn");
		latestLearnedWordTable.getColumnFormatter().addStyleName(1,
				"secondColumn");
		latestLearnedWordTable.getColumnFormatter().addStyleName(2,
				"thirdColumn");

		this.setHeight(350);
		this.setWidth(600);
	}

	public void addLatestLearnedWord(LearnedWord learnedWord) {
		// TODO Auto-generated method stub
		int row = latestLearnedWordTable.insertRow(1);
		Panel p = new Panel();
		p.setBorder(false);
		Hyperlink wordLink = new Hyperlink("",true,"");
		p.add(wordLink);
		
		final LearnedWord w = learnedWord;
		wordLink.setText(learnedWord.getWord().getWordEn());
		
		latestLearnedWordTable.setWidget(row, 0, p);
		latestLearnedWordTable.setText(row, 1, ""
				+ learnedWord.getLearnedTimes());
		latestLearnedWordTable.setText(row, 2, ClientUtil.DEGREES[learnedWord
				.getDegree()]);
		
		ToolTip tip = new ToolTip();
		
		tip.setTitle(w.getWord().getWordEn());
		tip.setClosable(false);
		tip.setAutoHide(true);
		tip.setDraggable(false);
		
		List<CnAndSentence> csList = w.getWord().getCnAndSentenceList();
		StringBuffer sb = new StringBuffer();
		sb.append("<div><span class='phonetics'>");
		sb.append(w.getWord().getPhonetics());
		sb.append("</span></div><ul>");
		
		for(int i = 0;i < csList.size();i++){
			sb.append("<li>");
			sb.append(csList.get(i).getCn());
			sb.append("</li>");
		}
		sb.append("</ul>");
		
		tip.setHtml(sb.toString());
		tip.applyTo(p);
		
		wordLink.addClickListener(new ClickListener(){

			public void onClick(Widget arg0) {
				// TODO Auto-generated method stub
				lessonOperator.displayWordInfo(w.getWord());
			}
		});
		
	}
}
